package cn.webrx;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@SpringBootApplication
@RestController
public class App implements ApplicationRunner {
    @Value("${server.port}")
    private int port;

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @GetMapping
    public String index(HttpSession session) {
        System.out.println(session.getMaxInactiveInterval());
        Enumeration<String> ns = session.getAttributeNames();
        while (ns.hasMoreElements()) {
            String n = ns.nextElement();
            System.out.println(ns);
            System.out.println(session.getAttribute(n));
        }
        return String.format("(中文字符串)Hello SpringBoot Version:%s", SpringBootVersion.getVersion());
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello(哈罗) %s!", name);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Runtime.getRuntime().exec("cmd /k start http://localhost:8080");
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "安全退出";
    }
}
